package com.zhoushihao.class0811;

class DepositException extends RuntimeException{
    DepositException(String msg){
        super(msg);
    }
}
class WithdrawException extends RuntimeException{
    WithdrawException(String msg){
        super(msg);
    }
}


public class BankAccount {

    private String accountNumber;   // 账号
    private String ownerName;       // 开户人姓名
    private double balance;         // 余额
    private String[] transactions;  // 交易记录
    private int transactionCount;   // 已记录交易数量
    private double limit = 0;


    // 构造方法
    public BankAccount(String accountNumber, String ownerName, double initialBalance, int maxTransactions) {
        this.accountNumber = accountNumber;
        this.ownerName = ownerName;
        this.balance = initialBalance;
        this.transactions = new String[maxTransactions];
        this.transactionCount = 0;
        addTransaction("开户，初始余额: " + initialBalance);
    }

    // 获取余额
    public double getBalance() {
        return balance;
    }

    // 存款
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            addTransaction("存款: +" + amount);
        } else {
            throw new DepositException("存款金额必须大于 0");
        }
    }

    // 取款
    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            addTransaction("取款: -" + amount);
        } else {
            throw new WithdrawException("取款失败，金额无效或余额不足");
        }
    }

    // 添加交易记录
    private void addTransaction(String record) {
        if (transactionCount < transactions.length) {
            transactions[transactionCount] = record;
            transactionCount++;
        } else {
            System.out.println("交易记录已满，无法记录更多交易");
        }
    }

    // 打印交易记录
    public void printTransactions() {
        System.out.println("账户 [" + accountNumber + "] 的交易记录：");
        for (int i = 0; i < transactionCount; i++) {
            System.out.println((i + 1) + ". " + transactions[i]);
        }
    }

    public void transfer(BankAccount target, double amount) {
        if (target == null) {
            System.out.println("目标账户不存在");
        } else if (amount <= 0) {
            System.out.println("转账金额必须大于0");
        } else if (amount > this.balance) {
            System.out.println("转账失败：余额不足");
        } else {
            limit += amount;
            if (limit > 3000) {
                System.out.println("当前转账累计已超过限额");
                limit -= amount;
            } else {
                this.balance -= amount;
                target.balance += amount;
                System.out.println("转账成功：" + this.accountNumber + " → " + target.accountNumber + " 金额: " + amount);
                this.addTransaction("向账户[" + target.accountNumber + "] 转账: -" + amount);
                target.addTransaction("来自账户[" + this.accountNumber + "] 的转账: +" + amount);
            }
        }
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount("1001", "Alice", 1000.0, 10);

        try {
            account.deposit(-500.0);
        } catch (DepositException e) {
            e.printStackTrace();
        } finally {
            try {
                account.withdraw(500.0);
            } catch (WithdrawException e) {
                e.printStackTrace();
            } finally {
                try {
                    account.withdraw(1500.0); // 测试余额不足
                } catch (WithdrawException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("当前余额: " + account.getBalance());
        account.printTransactions();
    }
}

